import java.util.ArrayList;
import java.util.List;

public class PreorderTraversal {
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        preorderTraversalChild(root, list);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }

    private void preorderTraversalChild(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        list.add(root.val);
        preorderTraversalChild(root.left, list);
        preorderTraversalChild(root.right, list);
    }
}
